package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FaultDb;
import com.ruoyi.system.service.IFaultDbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 故障数据库Controller
 *
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/system/db")
public class FaultDbController extends BaseController
{
    @Autowired
    private IFaultDbService faultDbService;

    /**
     * 查询故障数据库列表
     */
    @PreAuthorize("@ss.hasPermi('system:db:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultDb faultDb)
    {
        startPage();
        List<FaultDb> list = faultDbService.selectFaultDbList(faultDb);
        return getDataTable(list);
    }

    /**
     * 查询组别
     */
    @GetMapping("/getGroups")
    public List<String> getDistinctGroups(){
        return faultDbService.getDistinctGroups();
    }

    /**
     * 导出故障数据库列表
     */
    @PreAuthorize("@ss.hasPermi('system:db:export')")
    @Log(title = "故障数据库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultDb faultDb)
    {
        List<FaultDb> list = faultDbService.selectFaultDbList(faultDb);
        ExcelUtil<FaultDb> util = new ExcelUtil<FaultDb>(FaultDb.class);
        util.exportExcel(response, list, "故障数据库数据");
    }

    /**
     * 获取故障数据库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:db:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(faultDbService.selectFaultDbById(id));
    }

    /**
     * 新增故障数据库
     */
    @PreAuthorize("@ss.hasPermi('system:db:add')")
    @Log(title = "故障数据库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaultDb faultDb)
    {
        return toAjax(faultDbService.insertFaultDb(faultDb));
    }

    /**
     * 修改故障数据库
     */
    @PreAuthorize("@ss.hasPermi('system:db:edit')")
    @Log(title = "故障数据库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaultDb faultDb)
    {
        return toAjax(faultDbService.updateFaultDb(faultDb));
    }

    /**
     * 删除故障数据库
     */
    @PreAuthorize("@ss.hasPermi('system:db:remove')")
    @Log(title = "故障数据库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(faultDbService.deleteFaultDbByIds(ids));
    }
}
